import { listMsg,listMsgHis, updateMsgStatus,updateMsgStatusByTime, getNewMsgCount,replyMsg,delMsg,showMoreNewMessage,updateSeekHelpInfoPushStatus,analysisIndex } from '../services/msg';
import { routerRedux } from 'dva/router';
export default {
  /*namespace 是 model state 在全局 state 所用的 key，state 是默认数据*/
  namespace: 'msg',

  state: {
    loadingHis:true,
    msgHisData: {
      errorCode:0,
      msg:"",
      data:{
        pageNo:null,
        total:null,
        pageCount:null,
        pageSize:null,
        latestTime:null,
        list:[]
      },
    },
  },

  effects: {
    /*call 和 put 都是 redux-saga 的 effects，call 表示调用异步函数，put 表示 dispatch action，*/
    /**
     * 查看导播待审核列表
     * @param payload
     * @param callback
     * @param call
     * @param put
     */
    *listAuditMsg({ payload,callback }, { call, put }) {
      const response = yield call(listMsg, payload);
      if (callback) callback(response);
    },
    /**
     * 查看主播带播放列表
     * @param payload
     * @param call
     * @param put
     */
    *listPlayMsg({ payload ,callback}, { call, put }) {
      const response = yield call(listMsg, payload);
      if (callback) callback(response);
    },
    /**
     * 查看历史消息列表
     * @param payload
     * @param call
     * @param put
     */
    *listMsgHis({ payload }, { call, put }) {
      yield put({
        type: 'changeLoadingHis',
        payload: true,
      });
      const response = yield call(listMsgHis,payload);
      yield put({
        type: 'listMsgHisSuccess',
        payload: response,
      });
      yield put({
        type: 'changeLoadingHis',
        payload: false,
      });
    },
    /**
     * 更新消息状态
     * @param payload
     * @param callback
     * @param call
     * @param put
     */
    *updateMsgStatus({ payload, callback }, { call, put }) {
      const response = yield call(updateMsgStatus, payload);
      if (callback) callback(response);
    },
    /**
     * 更新指定时间之前的消息状态
     * @param payload
     * @param callback
     * @param call
     * @param put
     */
    *updateMsgStatusByTime({ payload, callback }, { call, put }) {
      const response = yield call(updateMsgStatusByTime, payload);
      if (callback) callback(response);
    },
    /**
     * 获取新消息数量
     * @param payload
     * @param callback
     * @param call
     * @param put
     */
    *getNewMsgCount({ payload, callback }, { call, put }) {
      const response = yield call(getNewMsgCount, payload);
      if (callback) callback(response);
    },
    /**
     * 获取新消息列表
     * @param payload
     * @param callback
     * @param call
     * @param put
     */
    *showMoreNewMessage({ payload, callback }, { call, put }) {
      const response = yield call(showMoreNewMessage, payload);
      if (callback) callback(response);
    },
    *updateSeekHelpInfoPushStatus({ payload, callback }, { call, put }) {
      const response = yield call(updateSeekHelpInfoPushStatus, payload);
      if (callback) callback(response);
    },
    /**
     * 消息回复
     * @param payload
     * @param callback
     * @param call
     * @param put
     */
    *replyMsg({ payload, callback }, { call, put }) {
      const response = yield call(replyMsg, payload);
      if (callback) callback(response);
    },

    *delMsg({ payload, callback }, { call, put }) {
      const response = yield call(delMsg, payload);
      if (callback) callback(response);
    },

    *analysisIndex({ payload, callback }, { call, put }) {
      const response = yield call(analysisIndex, payload);
      if (callback) callback(response);
    },
    *goToTargetPage({ payload }, { put }) {
      if (payload.pageTyep == 0) {
        yield put(routerRedux.push('/msg/msgHis'));
      }else if (payload.pageTyep == 1){
        yield put(routerRedux.push('/msg/auditMsg'));
      }else if (payload.pageTyep == 2){
        yield put(routerRedux.push('/msg/playMsg'));
      }else if (payload.pageTyep == 3){
        yield put(routerRedux.push('/follower/followerList'));
      }
    },
  },
  /*reducer 是唯一可以更新 state 的地方，接收参数 state 和 action，返回新的 state，*/
  reducers: {
    listMsgHisSuccess(state, action) {
      return {
        ...state,
        msgHisData: action.payload,
      };
    },
    changeLoadingHis(state, action) {
      return {
        ...state,
        loadingHis: action.payload,
      };
    },
  },
};
